package com.ysstech.reportworld.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysstech.reportworld.entity.TableIndexed;
import com.ysstech.reportworld.enums.ReportWorldStatusEnum;
import com.ysstech.reportworld.mapper.TableIndexedMapper;
import com.ysstech.reportworld.service.TableIndexedService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * <p>
 * 表对应索引信息 服务实现类
 * </p>
 *
 * @author lishuangliang
 * @since 2022-11-28
 */
@Service
public class TableIndexedServiceImpl extends ServiceImpl<TableIndexedMapper, TableIndexed> implements TableIndexedService {

    @Override
    public Map<String, List<TableIndexed>> getTableIndexedMap() throws Exception {
        // 索引信息
        QueryWrapper<TableIndexed> tableIndexedQueryWrapper = new QueryWrapper<>();
        tableIndexedQueryWrapper.in("STATUS", ReportWorldStatusEnum.getUpgradeToolsZipStatus());
        tableIndexedQueryWrapper.orderByAsc("INDEXED_SEQUENCE,INDEXED_COLUMN_SEQUENCE");
        List<TableIndexed> tableIndexedList = this.list(tableIndexedQueryWrapper);
        if (CollectionUtils.isEmpty(tableIndexedList)) {
            log.error("没有获取到索引信息！");
            return null;
        }
        // 索引处理
        Map<String, List<TableIndexed>> mapTableIndexed = new LinkedHashMap<>();
        for (TableIndexed indexed : tableIndexedList) {
            if (null == indexed || StringUtils.isBlank(indexed.getTableId())) {
                continue;
            }
            if (mapTableIndexed.containsKey(indexed.getTableId())) {
                List<TableIndexed> list = mapTableIndexed.get(indexed.getTableId());
                list.add(indexed);
                mapTableIndexed.put(indexed.getTableId(), list);
            } else {
                List<TableIndexed> list = new ArrayList<>();
                list.add(indexed);
                mapTableIndexed.put(indexed.getTableId(), list);
            }
        }
        return mapTableIndexed;
    }
}
